// pages/hackson_signup/hackson_signup.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dimen: [],
    marathonId: "",
    submit_button: false
  },

  plus: function () {
    var dimen_temp = this.data.dimen
    dimen_temp.push("")

    this.setData({
      dimen: dimen_temp
    })
  },

  substract: function (e) {
    var dimen_temp = this.data.dimen
    var index = e.currentTarget.dataset.index

    dimen_temp.splice(index, 1)

    this.setData({
      dimen: dimen_temp
    })
  },

  dimen_input: function (e) {
    var index = e.currentTarget.dataset.index
    var title = e.detail.value

    var dimen_temp = this.data.dimen
    dimen_temp[index] = title

    this.setData({
      dimen: dimen_temp
    })
  },

  formSubmit: function (e) {
    var that = this
    that.setData({
      submit_button: true
    })
    wx.showLoading({
      title: '注册中',
    })

    console.log(e.detail.value);
    var list_obj = e.detail.value

    var students = []

    for (var item in list_obj) {
      if (list_obj[item] == "") {
        wx.hideLoading()
        that.setData({
          submit_button: false
        })
        wx.showModal({
          title: '哎呀～',
          content: "有的选项没有填呢～",
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        return
      } else if (item != "name" && item != "detail" && item != "captain") {
        students.push(item)
      } else if (item == "captain") {
        students.push(list_obj[item])
      }
    }

    var id = ""
    var auth = getApp().globalData.kind
    if (auth == "1") {
      id = getApp().globalData.user.studentNo
    } else if (auth == "2") {
      id = getApp().globalData.user.teacherNo
    } else if (auth == "3") {
      id = getApp().globalData.user.adminNo
    } else if (auth == "4") {
      id = getApp().globalData.user.donatorNo
    }
    console.log(students, id, students.includes(id))
    if (!students.includes(id)){
      wx.hideLoading()
      that.setData({
        submit_button: false
      })
      wx.showModal({
        title: '提示',
        content: "不可以替别人报名",
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return
    } else {
      //检测学号是否已注册
      wx.request({
        url: getApp().globalData.my_server + "/hackson/check_studentNo_when_group_sign_up",
        data: {
          students: students.toString()
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          console.log(res)
          if (res.data.error_no != 0) {
            wx.hideLoading()
            that.setData({
              submit_button: false
            })
            wx.showModal({
              title: '哎呀～',
              content: res.data.error_msg,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else if (res.data.error_no == 0) {

            //检测项目名称是否已注册
            wx.request({
              url: getApp().globalData.my_server + "/hackson/check_id_for_group",
              data: {
                marathon_id: that.data.marathonId, //id1
                name: list_obj.name, //id2
              },
              method: "POST",
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              success: function (res) {
                console.log(res)
                if (res.data.error_no != 0) {
                  wx.hideLoading()
                  that.setData({
                    submit_button: false
                  })
                  wx.showModal({
                    title: '哎呀～',
                    content: res.data.error_msg,
                    success: function (res) {
                      if (res.confirm) {
                        console.log('用户点击确定')
                      } else if (res.cancel) {
                        console.log('用户点击取消')
                      }
                    }
                  })
                } else if (res.data.error_no == 0) {
                  //队伍报名
                  wx.request({
                    url: getApp().globalData.my_server + "/hackson/sign_up",
                    data: {
                      marathon_id: that.data.marathonId, //id1
                      name: list_obj.name, //id2
                      members: students.toString(),
                      captain: list_obj.captain,
                      detail: list_obj.detail
                    },
                    method: "POST",
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded"
                    },
                    success: function (res) {
                      console.log(res)
                      if (res.data.error_no != 0) {
                        wx.hideLoading()
                        that.setData({
                          submit_button: false
                        })
                        wx.showModal({
                          title: '哎呀～',
                          content: res.data.error_msg,
                          success: function (res) {
                            if (res.confirm) {
                              console.log('用户点击确定')
                            } else if (res.cancel) {
                              console.log('用户点击取消')
                            }
                          }
                        })
                      } else if (res.data.error_no == 0) {
                        wx.hideLoading()
                        wx.navigateBack({
                          delta: 1
                        })
                      }
                    },
                    fail: function (res) {
                      wx.hideLoading()
                      that.setData({
                        submit_button: false
                      })
                      wx.showModal({
                        title: '哎呀～',
                        content: '检测学号是否以注册失败',
                        success: function (res) {
                          if (res.confirm) {
                            console.log('用户点击确定')
                          } else if (res.cancel) {
                            console.log('用户点击取消')
                          }
                        }
                      })
                      console.log("fail!", res)
                    },
                    complete: function (res) {

                    }
                  })
                }
              },
              fail: function (res) {
                wx.hideLoading()
                that.setData({
                  submit_button: false
                })
                wx.showModal({
                  title: '哎呀～',
                  content: '检测项目名称是否已注册失败',
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
                console.log("fail!", res)
              },
              complete: function (res) {

              }
            })
          }
        },
        fail: function (res) {
          wx.hideLoading()
          that.setData({
            submit_button: false
          })
          wx.showModal({
            title: '哎呀～',
            content: '检测学号是否已注册失败',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
          console.log("fail!", res)
        },
        complete: function (res) {

        }
      })
    }


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      marathonId: options.marathonId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})